<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">提货计划单</label>
                    <div class="layui-input-block">
                        <input type="text" name="draw_plan_sn" placeholder="请输入提货计划单号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">PO单号</label>
                    <div class="layui-input-block">
                        <input type="text" name="po" placeholder="请输入PO单号" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px;">最早收货日期</label>
                    <div class="layui-input-block layui-jy-datetime" style="margin-left: 130px">
                        <input type="text" name="first_arrive_time" id="first_arrive_time" placeholder="请输入最早收货日期" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px;">最晚收货日期</label>
                    <div class="layui-input-block layui-jy-datetime" style="margin-left: 130px">
                        <input type="text" name="last_arrive_time" id="last_arrive_time" placeholder="请输入最晚收货日期" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status" class="selector">
                            <option value="">全部</option>
                            <option value="-1">未确定</option>
                            <option value="1">已确定</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button style="display:none" class="down" lay-submit="" lay-filter="LAY-list-down">下载</button>
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search">搜索</button>
                    <button type="reset" class="reset layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-list"  lay-filter="LAY-list"></table>
            <!-- 头部工具栏 -->
            <script type="text/html" id="toolbar">
                <div class="layui-btn-group fl layui-btn-group-jy">
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-upload-circle" lay-event="upload">&nbsp;导入</a>
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle" lay-event="export">&nbsp;导出</a>
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-add-circle-fine" lay-event="adds">&nbsp;批量确定</a>
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-refresh" lay-event="update">&nbsp;更新异常</a>
                </div>
            </script>
            <!-- 行内工具 -->
            <script type="text/html" id="LAY-list-toolbar">
                {{# if(d.status == -1){ }}
                <!--<a class="layui-btn layui-btn-xs" lay-event="add">确定</a>-->
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                {{# }else{ }}
                <a class="layui-btn layui-btn-、primary layui-btn-xs" lay-event="detail">详情</a>
                <a class="layui-btn layui-btn-disabled layui-btn-xs" lay-event="">删除</a>
                {{# } }}

            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form','toolJ','laydate','upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , toolJ = layui.toolJ
            , laydate = layui.laydate
            , form = layui.form
            , user = layui.data('yujiso');
        //日期时间范围
        laydate.render({
            elem: '#first_arrive_time'
            ,type: 'datetime'
            ,range: false
        });
        laydate.render({
            elem: '#last_arrive_time'
            ,type: 'datetime'
            ,range: false
        });
        //用户管理
        table.render({
            elem: '#LAY-list'
            ,url: '/index/Drawplan/getDrawplanList'
            ,toolbar: '#toolbar'
            ,cellMinWidth:80
            ,defaultToolbar: ['filter']
            ,cols: [[
                {type:'checkbox'}
                ,{field: 'draw_plan_sn', title: '提货计划单号'}
                ,{field: 'po', title: 'PO单号', width: 110}
                ,{field: 'sku', title: 'SKU'}
                ,{field: 'sku_name', title: '中文名称'}
                ,{field: 'po_quantity', title: 'PO数量'}
                ,{field: 'remaining_quantity', title: '提货结余'}
                ,{field: 'present_quantity', title: '本次提货数量'}
                ,{field: 'first_arrive_time', title: '最早收货日期'}
                ,{field: 'last_arrive_time', title: '最晚收货日期'}
                ,{field: 'upload', title: '上传人'}
                ,{field: 'operator', title: '修改人'}
                ,{field: 'update_time', title: '修改时间'}
                ,{field: 'status_name', title: '状态'}
                ,{fixed: 'right', align:'center',title:'操作', toolbar: '#LAY-list-toolbar', width:150}
            ]]
            ,page: true
            ,limit: 20
            ,text: {none: '暂无相关数据'}
        });
        //监听头部工具栏 操作
        table.on('toolbar(LAY-list)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list')
                , data = checkStatus.data;
            //event对应模板的
            switch (obj.event) {
                case 'export':
                    var field = obj.config.where;
                    toolJ.ajax({"data": field,"url":"/index/Drawplan/export"});
                    break;
                case 'update':
                    toolJ.ajax({"data": [],"url":"/tasks/Purchaseplan/syncNewErpPrToYujiso"});
                    break;
                case 'adds':
                    var ids = []; // 选中的ID
                    var true_ids = [];
                    $.each(data,function (index,item) {
                        ids.push(item.id);
                        if (item.status === 1) true_ids.push(item.draw_plan_sn);
                    });
                    if (ids.length <= 0){
                        layer.msg("请勾选需要确定的提货计划", {icon: 2});
                        return false;
                    }
                    true_ids = true_ids.join(); // 转成字符串
                    if (true_ids.length > 0){
                        layer.msg("提货计划:"+true_ids+"已确定，请勿重复确定", {icon: 2});
                        return false;
                    }
                    layer.confirm('你真的要确定这些数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":ids},"url":"/index/Drawplan/sureDrawplan"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
                    break;
                case 'upload':
                    var url = "/index/Drawplan/upload";
                    var csvUrl = "/template/importcvs/drawplan.csv";
                    toolJ.upload(url,csvUrl,function (res) {
                        if (res.code == 1) $(".search").click();
                    });
                    break;

            }
        });

        //监听行内操作
        table.on('tool(LAY-list)', function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'disable':
                    toolJ.ajax({"data":{"id":data.id,"status":-1},"url":"/index/Drawplan/disableOrEnable"});
                    break;
                case 'enable':
                    toolJ.ajax({"data":{"id":data.id,"status":1},"url":"/index/Drawplan/disableOrEnable"});
                    break;
                case 'edit':
                    admin.popup({
                        title: '编辑'
                        ,area: ['800px', '600px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                            view(this.id).render('Drawplan/edit',data).done(function(){
                                $.ajax({
                                    type: 'get',
                                    url:"/index/Drawplan/edit?id="+data.id,
                                    dataType: "json",
                                    success: function(res){
                                        if (res.code === 1){
                                            var html_ = "";
                                            $.each(res.data,function (index,item) {
                                                html_ += '<tr>' +
                                                    '<input type="hidden" name="detai_id[]" value="'+item.id+'">' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.base_country_name+'</label></td>' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.base_shop_name+'</label></td>' +
                                                    '<td><input style="text-align: left" lay-verify="required" placeholder="请输入理论需求数量" onkeyup="clearNoNum_(this)" name="need_quantity[]" type="text" value="'+item.need_quantity+'" class="layui-input"></td>' +
                                                    '<td><input style="text-align: left" lay-verify="required" placeholder="请输入实际提货数量" onkeyup="clearNoNum_(this)" name="actual_quantity[]" type="text" value="'+item.actual_quantity+'" class="layui-input"></td>' +
                                                    '</tr>';
                                            });
                                            var date_value = data.first_arrive_time+" - "+data.last_arrive_time;
                                            //日期时间范围
                                            laydate.render({
                                                elem: '#th_time'
                                                ,type: 'datetime'
                                                ,value:date_value
                                                ,range: true
                                            });
                                            $('#draw_body').append(html_);
                                            $('#id').val(data.id);
                                            $('#po_').val(data.po);
                                            $('#sku_').val(data.sku);
                                            form.render(null, 'layui-drawplan-edit-form-list');
                                            //监听提交
                                            form.on('submit(layuiadmin-app-form-submit)', function(data){
                                                var field = data.field; //获取提交的字段
                                                field['operator'] = user.cn_name;
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                toolJ.ajax({"data":field,"url":"/index/Drawplan/edit"});
                                                layer.close(index); //执行关闭
                                                table.reload();
                                            });
                                        }else {
                                            layer.msg(data.msg, {icon: 2});
                                        }
                                    }
                                    ,error:function () {
                                        layer.msg('系统异常', {icon: 2});
                                    }
                                });

                            });
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('你确定要删除该条数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":data.id},"url":"/index/Drawplan/del"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
                    break;
                case 'add':
                    layer.confirm('你真的要确定该条数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":[data.id]},"url":"/index/Drawplan/sureDrawplan"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
                    break;
                case 'detail':
                    admin.popup({
                        title: '详情'
                        ,area: ['800px', '600px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                            view(this.id).render('Drawplan/edit',data).done(function(){
                                $.ajax({
                                    type: 'get',
                                    url:"/index/Drawplan/edit?id="+data.id,
                                    dataType: "json",
                                    success: function(res){
                                        if (res.code === 1){
                                            var html_ = "";
                                            $.each(res.data,function (index,item) {
                                                html_ += '<tr>' +
                                                    '<input type="hidden" name="detai_id[]" value="'+item.id+'">' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.base_country_name+'</label></td>' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.base_shop_name+'</label></td>' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.need_quantity+'</label></td>' +
                                                    '<td><label style="text-align: left" class="layui-form-label">'+item.actual_quantity+'</label></td>' +
                                                    '</tr>';
                                            });
                                            var date_value = data.first_arrive_time+" - "+data.last_arrive_time;
                                            //日期时间范围
                                            laydate.render({
                                                elem: '#th_time'
                                                ,type: 'datetime'
                                                ,value:date_value
                                                ,range: true
                                            });
                                            $('#draw_body').append(html_);
                                            $('#id').val(data.id);
                                            $('#po_').val(data.po);
                                            $('#sku_').val(data.sku);
                                            $('#surebtn').val("返回");
                                            $('#th_time').attr('disabled','disabled');
                                            form.render(null, 'layui-drawplan-edit-form-list');
                                            //监听提交
                                            form.on('submit(layuiadmin-app-form-submit)', function(data){
                                                layer.close(index); //执行关闭
                                            });
                                        }else {
                                            layer.msg(data.msg, {icon: 2});
                                        }
                                    }
                                    ,error:function () {
                                        layer.msg('系统异常', {icon: 2});
                                    }
                                });

                            });
                        }
                    });
                    break;
            }
        });
        /*重置数据，刷新列表*/
        $('.reset').on('click', function () { //
            $("input").val('');
            $("textarea").val('');
            $(".selector").val('');
            $('.search').click();
        });
        /*重新渲染界面*/
        form.render(null, 'LAY-list-form');
    });
</script>